﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WinFormsApp
{

    public class Win32WindowApi
    {
        //定义回调函数的委托
        public delegate bool CALLBACK(int hwnd, int lparm);


        //用于获取前台窗口句柄,设置当前窗口句柄
        [DllImport("user32.dll")]
        public static extern int EnumWindows(CALLBACK x, int y);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);

        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        //是否可显示
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool IsWindowVisible(int hWnd);

        //是否处于活动状态
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool IsWindowEnabled(int hWnd);

        [DllImport("user32")]
        public static extern int GetParent(int hwnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();

        //置顶
        // [DllImport("user32.dll")]
        // private static extern int BringWindowToTop(IntPtr hWnd);

        // 激活窗口
        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern int SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);

        //设置窗口位置：0=关闭，1=正常，2=最小化，3=最大化
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);


        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr FindWindow(string className, string windowName);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        //public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        public static extern IntPtr FindWindowEx(string className, string windowName);

        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        public static extern IntPtr ChildWindowFromPoint(IntPtr hwndParent, POINT pt);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr ChildWindowFromPointEx(IntPtr hParent, POINT pt, ChildFromPointFlags flags);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct PixelPoint
    {
        public int X;
        public int Y;
        public PixelPoint(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;

        public POINT(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

    public enum ChildFromPointFlags
    {
        CWP_ALL = 0x0000,
        CWP_SKIPINVISIBLE = 0x0001,
        CWP_SKIPDISABLED = 0x0002,
        CWP_SKIPTRANSPARENT = 0x0004
    }


}
